AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")

include("shared.lua")

function ENT:Initialize()
	self.Entity:SetModel("models/props_combine/health_charger001.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_NONE)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self:SetUseType(SIMPLE_USE)
	
	local phys = self.Entity:GetPhysicsObject()
	self.nodupe = true
	self.ShareGravgun = true

	if phys and phys:IsValid() then phys:Wake() end
	self:SetCollisionGroup(COLLISION_GROUP_WORLD) 
end

function ENT:Use(activator,caller)
	local Markup = 1 --cash per unit of Health
	local Purchase_Amount = math.floor((activator:GetPlayerClass().MaxHealth - activator:Health())* Markup) --how many units you need to fill Health
	local Purchase_Units = math.floor((activator:GetMoney()) / Markup) --how many units the player can purchase

	local Health_On = activator:Health() --players current Health count
	local Health_Max = activator:GetPlayerClass().MaxHealth --how many units of Health the player can have

	if(Health_On < Health_Max)then

		if(Purchase_Units >= Purchase_Amount )then
			local Total = Purchase_Amount
			
			activator:TakeMoney(Total)
			activator:SetHealth(Health_Max)
			GAMEMODE:Push(activator, "You have filled your health for $"..Total, 5)
			
		elseif (Purchase_Units < Purchase_Amount )then
			if(Purchase_Units >= 1)then
				local Total = math.floor(Purchase_Units * Markup)
				
				activator:TakeMoney(Total)
				activator:SetHealth((Health_On + Purchase_Units))
				
				if(activator:Health() > Health_Max)then
					activator:SetHealth(Health_Max)
					GAMEMODE:Push(activator, "You have filled your health for $"..Total, 5)
					
				elseif(activator:Health() < Health_Max)then
					GAMEMODE:Push(activator, "Purchased "..Purchase_Units.." units of health for $"..Total, 5)
				end --doubble checking is fun

			elseif(Purchase_Units <= 0)then
				GAMEMODE:Push(activator, "You're broke", 1)
			end
		end

	elseif(Health_On >= Health_Max)then
		--hey asshole save some for the rest of us
	end
end
